package com.RecipeBook.browse;

import java.io.IOException;
import java.util.ArrayList;

import com.RecipeBook.common.DataManager;
import com.RecipeBook.common.Recipe;
import com.RecipeBook.common.RecipeActivity;
import com.RecipeBook.common.RecipeInProgress;
import com.RecipeBook.cooking.CookingMain;
import com.RecipeBook.cooking.CookingRecipe;
import com.RecipeBook.main.GlobalsManager;
import com.RecipeBook.main.R;
import com.RecipeBook.search.Search;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.SQLException;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;



public class Browse extends	RecipeActivity {
	
	Recipe r;
	RecipeInProgress rip;
	DataBaseHelper myDbHelper;
	ProgressDialog pd;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.browse);


		// Prevents it from reloading the database everytime Browse is called
		if (GlobalsManager.getGlobals().getDatabaseHelper() == null) {
			final Browse th = this;

			// Here is the heavy-duty thread
			Thread t = new Thread() {
				public void run() {
					myDbHelper = new DataBaseHelper(th);

					try {
						myDbHelper.openDataBase();
						myDbHelper.prepareDB(pd);
						pd.dismiss();

					}
					catch(SQLException sqle){
						throw sqle;
					}

					myDbHelper.getRecipePKs();
					GlobalsManager.getGlobals().setDatabaseHelper(myDbHelper);
				}
			};
			pd = startProgress();
			t.start();
		}
	}

	public void imageClick(View view) {
		Toast pieceToast = Toast.makeText(getApplicationContext(), "Good Choice! We'll be redirecting you to the Cooking screen.", Toast.LENGTH_SHORT);
		pieceToast.show();
		GlobalsManager.getGlobals().getRecipesInProgress().add(new RecipeInProgress(myDbHelper.getRecipeFromId(116)));
		Intent intentS = new Intent(this, CookingMain.class);
		startActivity(intentS);

	}
}